package com.licenta.fbApp.client;

import java.util.ArrayList;
import java.util.HashMap;

import com.licenta.fbApp.client.LeftPanel;
import com.licenta.fbApp.shared.DatabaseService;
import com.licenta.fbApp.shared.DatabaseServiceAsync;
import com.licenta.fbApp.shared.FoodService;
import com.licenta.fbApp.shared.FoodServiceAsync;
import com.licenta.fbApp.shared.HomeService;
import com.licenta.fbApp.shared.HomeServiceAsync;
import com.licenta.fbApp.shared.NotLoggedInException;
import com.licenta.fbApp.shared.Restaurant;
import com.licenta.fbApp.shared.TipsService;
import com.licenta.fbApp.shared.TipsServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;

public class AppPage extends Composite {
  private static final String HOME_URL = "img/1_home.png";
  private static final String HOME_URL_S = "img/1_home_small.png";
  private static final String TIPS_URL = "img/3_tips.png";
  private static final String TIPS_URL_S = "img/3_tips_small.png";
  private static final String FOOD_URL = "img/4_food.png";
  private static final String FOOD_URL_S = "img/4_food_small.png";
 
	private HomeServiceAsync homeService = GWT.create(HomeService.class);
	private FoodServiceAsync foodService = GWT.create(FoodService.class);
	private TipsServiceAsync tipsService = GWT.create(TipsService.class);
	
	private ArrayList<Restaurant> DB = new ArrayList<Restaurant>();  
	private HashMap<Integer, String>  sim = new HashMap<Integer, String>();
	private DatabaseServiceAsync dbService = GWT.create(DatabaseService.class);
	
	private final HomePageTab homePage = new HomePageTab();
	private final FoodPageTab foodPage = new FoodPageTab();
	private final TipsPageTab tipsPage = new TipsPageTab();
	
  public AppPage(Long uid, boolean isUsing) {
	  
	final LeftPanel leftPanel = new LeftPanel(uid);

	// Initialize the service proxy.
    if (foodService == null) {
    	foodService = GWT.create(FoodService.class);
    }
    final ImageTabPanel p = new ImageTabPanel();
    

	try {
		foodService.getInfo(DisplayUserInfo.getUid(), new AsyncCallback<ArrayList<Restaurant>>() {
	
	      public void onSuccess(ArrayList<Restaurant> result) {        	  
	    	  setDB(result); 
	    	  foodPage.set(DB); 
	    	  tipsPage.set(DB);
	    	  homePage.set(DB);	    	  
	      }		      
		  public void onFailure(Throwable caught) {
			  System.err.println("failure on logon : " + caught);
		      }
		    });
	  } catch (NotLoggedInException e) {
	    e.printStackTrace();
	  }
		  
		  final int users = 1062;
		  final int dbSize = 532;
		  	  
		  for (int i = 0; i < dbSize; i+= 100) {
			tipsService.getLikes(i, i + 100, new AsyncCallback<HashMap<Integer, String>>() {
			        public void onSuccess(HashMap<Integer, String> result) {
			      	  sim.putAll(result);
			      	  tipsPage.setLikes(sim);
			        }	
				  
				  public void onFailure(Throwable caught) {
					  System.err.println("failure: " + caught);
				      }
				});
		  }
		  
		
    /**
     *  Get list of restaurants from database
     */
    /*
    dbService.getRestaurants("select count(*) from restaurants", new AsyncCallback<ArrayList<Restaurant>>() {
        
        public void onSuccess(ArrayList<Restaurant> result) { 
      	  try {
      		setDB(result);
			foodPage.set(result);
      	  } catch (Exception e) {
			System.err.println(e.getMessage());
      	  }
        }	
	  
	  public void onFailure(Throwable caught) {
		  System.err.println("failure on logon : " + caught);
	      }
	    });    
	*/
	/**
	 *  Initialize application
	 */	
	  	p.add(homePage, p.new ImageTab("Home", HOME_URL, HOME_URL_S));

	  	p.add(tipsPage, p.new ImageTab("Tips", TIPS_URL, TIPS_URL_S));
	  	
	  	p.add(foodPage, p.new ImageTab("Food", FOOD_URL, FOOD_URL_S));
	  	
		  DockLayoutPanel dock = new DockLayoutPanel(Unit.PX);
		  dock.addWest(leftPanel, 200);
		  dock.add(p);		  
		  initWidget(dock);
		  System.out.println("widget initialized");
  }
  
  private void setDB(ArrayList<Restaurant> result) {
	  this.DB = result;
	  System.out.println("db set "+result.size());
	  
	  homeService.getTop(DB,new AsyncCallback<ArrayList<String>>() {
	        public void onSuccess(ArrayList<String> result) {
		      	  homePage.add(result);
		        }	
			  
			  public void onFailure(Throwable caught) {
				  System.err.println("failure: " + caught);
			      }
			});
  }
}
